package com.winz.cultureapp.http.restful;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.winz.cultureapp.constants.Constants;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 创建retrofit
 *
 */
public class RetrofitProvider {

    private final static int READ_TIME_OUT = 20;
    private final static int CONNECT_TIME_OUT = 20;

    public static class Factory {
        public static Api getApiService(){

//           Interceptor cacheIntercepter=new Interceptor() {
//               @Override
//               public Response intercept(Chain chain) throws IOException {
//                   //对request的设置用来指定有网/无网下所走的方式
//                   //对response的设置用来指定有网/无网下的缓存时长
//
//                   Request request = chain.request();
//                   if (!NetworkUtil.isNetAvailable(DemoApplication.getInstance())) {
//                       //无网络下强制使用缓存，无论缓存是否过期,此时该请求实际上不会被发送出去。
//                       //有网络时则根据缓存时长来决定是否发出请求
//                       request = request.newBuilder()
//                               .cacheControl(CacheControl.FORCE_CACHE).build();
//                   }
//
//                   Response response = chain.proceed(request);
//                   if (NetworkUtil.isNetAvailable(DemoApplication.getInstance())) {
//                       //有网络情况下，超过1分钟，则重新请求，否则直接使用缓存数据
//                       int maxAge = 60; //缓存一分钟
//                       String cacheControl = "public,max-age=" + maxAge;
//                       //当然如果你想在有网络的情况下都直接走网络，那么只需要
//                       //将其超时时间maxAge设为0即可
//                       return response.newBuilder()
//                               .header("Cache-Control",cacheControl)
//                               .removeHeader("Pragma").build();
//                   } else {
//                       //无网络时直接取缓存数据，该缓存数据保存1周
//                       int maxStale = 60 * 60 * 24 * 7 * 1;  //1周
//                       return response.newBuilder()
//                               .header("Cache-Control", "public,only-if-cached,max-stale=" + maxStale)
//                               .removeHeader("Pragma").build();
//                   }
//
//               }
//           };

//           File cacheFile = new File(DemoApplication.getInstance().getExternalCacheDir(), "HttpCache");//缓存地址
//           Cache cache = new Cache(cacheFile, 1024 * 1024 * 50); //大小50Mb

           HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();

           loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

           OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
           builder.addInterceptor(loggingInterceptor);
           builder.readTimeout(READ_TIME_OUT, TimeUnit.SECONDS);
           builder.connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS);
           // 缓存
//           builder.addNetworkInterceptor(cacheIntercepter);
//           builder.cache(cache);

//        Gson gson = new GsonBuilder()
//                .setLenient()
//                .create();

            Api api = new Retrofit.Builder().baseUrl(Constants.URL.ALL_PATH)
                    .client(builder.build())
                    .addConverterFactory(GsonConverterFactory.create(/*gson*/))
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build()
                    .create(Api.class);

            return api;
       }
//        public static Api getApiService(){
//
//            return getApiService(SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN,""));
//        }
//
//        public static Api getApiService(String token){
//            OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
//
//            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//            builder.addInterceptor(loggingInterceptor);
//
//            if (!TextUtils.isEmpty(token)){
//                Interceptor headerInterceptor = new Interceptor() {
//                    @Override
//                    public Response intercept(Chain chain) throws IOException {
//                        Request originalRequest = chain.request();
//                        Request.Builder builder = originalRequest.newBuilder();
//                        //设置具体的header内容
//                        builder.header("token", token);
//
//                        Request.Builder requestBuilder =
//                                builder.method(originalRequest.method(), originalRequest.body());
//                        Request request = requestBuilder.build();
//                        return chain.proceed(request);
//                    }
//                };
//                builder.addInterceptor(headerInterceptor);
//            }
//            builder.readTimeout(READ_TIME_OUT, TimeUnit.SECONDS);
//            builder.connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS);
//
//
//            return new Retrofit.Builder().baseUrl(Constants.URL.ALL_PATH)
//                    .client(builder.build())
//                    .addConverterFactory(GsonConverterFactory.create())
//                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                    .build()
//                    .create(Api.class);
//        }

    }
}
